﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLDBHYT.DAO;
using QLDBHYT.VO;
using QLDBHYT.BUS;



namespace QLDBHYT
{
    public partial class KCBNgoaiTru : Form
    {
        private P1hBUS p1hBUS;
        public KCBNgoaiTru()
        {
            InitializeComponent();
        }

        private void KCBNgoaiTru_Load(object sender, EventArgs e)
        {
            p1hBUS = new P1hBUS();
            GetKhoakham();
            GetThuoc();
            fa.Cols[2].Editor = cboThuoc;
            fa.Cols[2].Editor.Width = 800;
            
        }

        private void txtMathe_Leave(object sender, EventArgs e)
        {
            P1hVO p1h;
            p1h = p1hBUS.GetMathe(txtMathe.Text);
            txtHoten.Text = p1h.Hoten;
            txtGioitinh.Text = p1h.Gioitinh.ToString();
            txtNamsinh.Text = p1h.Namsinh.ToString();
            txtDiachi.Text = p1h.Diachi;
            txtGTTU.Value = p1h.Gttu;
            txtGTDen.Value = p1h.Gtden;
            txtMatinh.Text = p1h.Tinhkcb;
            txtMaBV.Text = p1h.Makcb;
            txtTenBV.Text = p1h.Benhvien;

        }

        private void KCBNgoaiTru_KeyDown(object sender, KeyEventArgs e)
        {
            Modules.SendKey(this, e);
            if (e.KeyCode == Keys.F11)
            {
                Cursor.Current = Cursors.WaitCursor;
                frmThemChiPhi childForm = new frmThemChiPhi();
                childForm.ShowDialog();
                Cursor.Current = Cursors.Default;
                
            }
        }

        private void btnMoi_Click(object sender, EventArgs e)
        {
            cboDT.SelectedIndex = 0;
            cboMakhoa.SelectedIndex = 0;
            txtCongkham.Value = 2000;
            
        }
        private void GetKhoakham()
        {

            cboMakhoa.ClearItems();
            cboMakhoa.AllowSort = true;
            cboMakhoa.DataSource = p1hBUS.GetKhoaKham();
            cboMakhoa.Splits[0].DisplayColumns[0].Width = 50;
            cboMakhoa.Splits[0].DisplayColumns[1].Width = 400;
            
            
          
        }
        private void GetThuoc()
        {
            cboThuoc.ClearItems();
            cboThuoc.AllowSort = true;
            cboThuoc.DataSource = p1hBUS.GetTenHieu(Convert.ToInt32 (Modules.GetKy().Rows[0]["thang"]), 
               Convert.ToInt32(Modules.GetKy().Rows[0]["nam"]));
            cboThuoc.Splits[0].DisplayColumns[0].Width = 400;
            cboThuoc.Splits[0].DisplayColumns[1].Width = 50;
            cboThuoc.Splits[0].DisplayColumns[2].Width = 50;

        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void SumNhomCP()
        {
            decimal decDM, decMau, decDC, decXN,
                decTNCN, decTT, decDVKTC, decVTYT,
                decTH, decNG, decVC,decVTYTTT, decThuocK;
                decDM= decMau= decDC= decXN=
                decTNCN= decTT= decDVKTC= decVTYT=
                decTH= decNG= decVC=decVTYTTT= decThuocK=0;

            for (int i = 1; i <= fa.Rows.Count - 1; i++)
            {
                if (fa.Rows[i]["mahieu"] != null)
                {
                    switch (fa.Rows[i]["MaNhomCP"].ToString())
                    {
                        case "01":
                            decDM = decDM + Convert.ToDecimal(fa.Rows[i]["thanhtien"]);
                            break;
                        case "02":
                            decMau = decMau + Convert.ToDecimal(fa.Rows[i]["thanhtien"]);
                            break;
                        case "03":
                            decDC = decDC + Convert.ToDecimal(fa.Rows[i]["thanhtien"]);
                            break;
                        case "04":
                            decXN = decXN + Convert.ToDecimal(fa.Rows[i]["thanhtien"]);
                            break;
                        case "05":
                            decTNCN = decTNCN + Convert.ToDecimal(fa.Rows[i]["thanhtien"]);
                            break;
                        case "06":
                        case "08":
                            decTT = decTT + Convert.ToDecimal(fa.Rows[i]["thanhtien"]);
                            break;
                        case "09":
                            decDVKTC = decDVKTC + Convert.ToDecimal(fa.Rows[i]["thanhtien"]);
                            break;
                        case "10":
                            decVTYT = decVTYT + Convert.ToDecimal(fa.Rows[i]["thanhtien"]);
                            break;
                        case "11":
                            decNG = decNG + Convert.ToDecimal(fa.Rows[i]["thanhtien"]);
                            break;
                        case "12":
                            decVC = decVC + Convert.ToDecimal(fa.Rows[i]["thanhtien"]);
                            break;
                        case "13":
                            decVTYTTT = decVTYTTT + Convert.ToDecimal(fa.Rows[i]["thanhtien"]);
                            break;
                        case "14":
                            decThuocK = decThuocK + Convert.ToDecimal(fa.Rows[i]["thanhtien"]);
                            break;
                    }
                }
            }
            txtDM.Value = decDM;
            txtMau.Value = decMau;
            txtDT.Value = decDC;
            txtXN.Value = decXN;
            txtCDHA.Value = decTNCN;
            txtTT.Value = decTT;
            txtDVKT.Value = decDVKTC;
            txtTieuHao.Value = decVTYT;
            txtThaythe.Value = decVTYTTT;
            txtThuocK.Value = decThuocK;
 
            decDM = decMau = decDC = decXN =
                decTNCN = decTT = decDVKTC = decVTYT =
                decTH = decNG = decVC = decVTYTTT = decThuocK = 0;
        }

        private void fa_AfterEdit(object sender, C1.Win.C1FlexGrid.RowColEventArgs e)
        {
            if(fa.Cols[e.Col].Name == "SL")
            {
                fa.Rows[e.Row]["thanhtien"] = Convert.ToInt32(fa.Rows[e.Row]["SL"]) * Convert.ToDecimal(fa.Rows[e.Row]["dongia"]);
                SumNhomCP();
            }
            if (fa.Cols[e.Col].Name == "TenCP")
            {
                fa.Rows[e.Row]["dongia"] = cboThuoc.Columns["gia"].Value;
                fa.Rows[e.Row]["giabhxh"] = cboThuoc.Columns["giabhxh"].Value;
                fa.Rows[e.Row]["MaNhomCP"] = cboThuoc.Columns["sttnh"].Value;
                fa.Rows[e.Row]["mahieu"] = cboThuoc.Columns["mahieu"].Value;
                switch (fa.Rows[e.Row]["MaNhomCP"].ToString())
                {
                    case "01":
                        fa.Rows[e.Row]["NhomCP"] = "Trong danh mục";
                        break;
                    case "02":
                        fa.Rows[e.Row]["NhomCP"] = "Máu, Đạm";
                        break;
                    case "03":
                        fa.Rows[e.Row]["NhomCP"] = "Dịch truyền";
                        break;
                    case "04":
                        fa.Rows[e.Row]["NhomCP"] = "Xét nghiệm";
                        break;
                    case "05":
                        fa.Rows[e.Row]["NhomCP"] = "CĐHA, TDCN";
                        break;
                    case "06":
                    case "08":
                        fa.Rows[e.Row]["NhomCP"] = "Thủ thuật";
                        break;
                    case "09":
                        fa.Rows[e.Row]["NhomCP"] = "DVKT cao";
                        break;
                    case "10":
                        fa.Rows[e.Row]["NhomCP"] = "VTYT tiêu hao";
                        break;
                    case "11":
                        fa.Rows[e.Row]["NhomCP"] = "Ngày giường";
                        break;
                    case "12":
                        fa.Rows[e.Row]["NhomCP"] = "Vận chuyển";
                        break;
                    case "13":
                        fa.Rows[e.Row]["NhomCP"] = "VTYT thay thế";
                        break;
                    case "14":
                        fa.Rows[e.Row]["NhomCP"] = "Thuốc K";
                        break;
                }
            }
        }

       
    }
}
